<template>
  <div class="app-container">
    <div>
      <!-- 查询 -->
      <div class="filter-container">
        <el-form :inline="true" class="search-form" label-width="100px">
          <el-row style="display: flex; flex-wrap: wrap">
            <div style="flex: 1">
              <el-form-item label="版本号" style="margin: 5px 0">
                <el-input
                  v-model="searchForm.version"
                  placeholder="请输入版本号"
                  clearable
                  show-word-limit
                />
              </el-form-item>
              <el-form-item label="是否启用" style="margin: 5px 0">
                <el-select
                  v-model="searchForm.status"
                  clearable
                  placeholder="请选择"
                >
                  <el-option
                    v-for="item in [
                      { value: 1, label: '启用' },
                      { value: 0, label: '禁用' },
                    ]"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </div>
          </el-row>
        </el-form>
      </div>
    </div>
    <el-divider></el-divider>
    <div style="margin: 20px 0 0 6px">
      <el-row :gutter="20" style="display: flex">
        <el-col>
          <el-button
            type="primary"
            size="small"
            icon="el-icon-circle-plus-outline"
            @click="addWay()"
            >添加新版本</el-button
          >
        </el-col>
      </el-row>
      <el-row :gutter="20" style="margin-top: 20px">
        <el-col>
          <el-table
            v-loading="loading"
            :data="dataList"
            :header-cell-style="{
              'background-color': '#f3f8fe',
              color: '#606266',
              height: '50px',
            }"
            style="width: 100%"
          >
            <el-table-column align="center" prop="created_at" label="创建时间">
            </el-table-column>
            <el-table-column align="center" prop="version" label="版本号">
            </el-table-column>
            <el-table-column align="center" prop="app_type" label="版本类型">
              <template slot-scope="scope">
                <span v-if="scope.row.app_type == 1"
                  >整包更新</span
                >
                <span v-else> 热更新</span>
              </template>
            </el-table-column>

            <el-table-column align="center" prop="apk_url" label="更新地址">
            </el-table-column>

            <el-table-column align="center" prop="content" label="更新说明">
              <template slot-scope="scope">
                {{ scope.row.content ? scope.row.content : "-" }}
              </template>
            </el-table-column>
            <el-table-column align="center" prop="status" label="是否启用">
              <template slot-scope="scope">
                <el-switch
                  v-model="scope.row.status"
                  :active-value="1"
                  :inactive-value="0"
                  @change="eidtStatusWay(scope.row)"
                />
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              fixed="right"
              width="240"
              prop="address"
              label="操作"
            >
              <template slot-scope="scope">
                <el-button
                  type="primary"
                  size="mini"
                  @click="editWay(scope.row)"
                >
                  编辑
                </el-button>
                <el-popconfirm
                  title="确定要删除该数据吗？"
                  @confirm="deleteWay(scope.row)"
                >
                  <el-button
                    style="margin-left: 10px"
                    type="danger"
                    size="mini"
                    slot="reference"
                  >
                    删除
                  </el-button>
                </el-popconfirm>
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
      <el-row style="text-align: right; margin-top: 20px">
        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="searchForm.page"
          :limit.sync="searchForm.size"
          @pagination="getListWay"
        />
      </el-row>
    </div>

    <el-dialog
      :close-on-click-modal="false"
      :title="title"
      :visible.sync="dialogVisible"
      width="700px"
      @close="handleClose"
    >
      <el-row style="position: relative" v-loading="dialogLoading">
        <el-form
          :model="form"
          :rules="rules"
          ref="form"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="更新类型" prop="app_type">
            <el-radio-group v-model="form.app_type">
              <el-radio :label="1">整包更新</el-radio>
              <el-radio :label="2">热更新</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="版本号" prop="version">
            <el-input
              placeholder="请输入版本号"
              maxlength="10"
              v-model="form.version"
            ></el-input>
          </el-form-item>
          <el-form-item label="上传文件" prop="apk_url">
            <fileUp :file="file" @uploadCallback="avatarCallback"></fileUp>
          </el-form-item>
          <el-form-item label="更新说明" prop="content">
            <editor-text
              v-model="form.content"
              :height="240"
              @input="editorclick"
            />
            <!-- <tinymce-editor v-model="form.content" :height="500" /> -->
          </el-form-item>
        </el-form>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button :loading="dialogLoading" @click="dialogVisible = false"
          >取 消</el-button
        >
        <el-button :loading="dialogLoading" type="primary" @click="submitWay"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getAppList, saveApp, editApp, delApp } from "@/api/admin/version";
import TinymceEditor from "@/components/Tinymce";
export default {
  components: {
    TinymceEditor,
  },
  data() {
    return {
      title: "",
      loading: false,
      searchForm: {
        page: 1,
        size: 10,
        status: "",
        version: "",
      },
      dataList: [],
      total: 0,
      dialogVisible: false,
      dialogLoading: false,
      avatarFile: {},
      form: {
        version: "",
        apk_url: "",
        content: "",
        app_type: 1,
      },
      file: {
        url: "",
      },
      contentBool: false,
      rules: {
        app_type: [
          {
            required: true,
            validator: (rule, value, callback) => {
              callback();
            },
            trigger: "change",
          },
        ],
        version: [
          {
            required: true,
            validator: (rule, value, callback) => {
              const req = /[^0-9.]/g;
              if (req.test(value)) {
                callback(
                  new Error(
                    '当前版本号格式不正确,应为数字和.组成,例如"1.0.0.1"'
                  )
                );
              } else {
                callback();
              }
            },
            trigger: "change",
          },
        ],
        apk_url: [
          {
            required: true,
            validator: (rule, value, callback) => {
              if (this.form.apk_url == "") {
                callback(new Error("请上传更新包"));
              } else {
                callback();
              }
            },
            trigger: "change",
          },
        ],
        content: [
          {
            required: true,
            validator: (rule, value, callback) => {
              if (value == "") {
                callback(new Error("请输入更新说明"));
              } else {
                callback();
              }
            },
            trigger: "change",
          },
        ],
      },
    };
  },
  created() {
    this.getListWay();
  },

  methods: {
    getListWay() {
      this.loading = true;
      getAppList(this.searchForm).then((res) => {
        this.loading = false;
        if (res.code !== 200 || res.status !== 1) {
          return this.$messages.error(res.msg);
        }
        this.dataList = res.data.list;
        this.total = res.data.total;
      });
    },
    deleteWay(row) {
      this.loading = true;
      delApp({ id: row.id })
        .then((res) => {
          if (res.code !== 200 || res.status !== 1) {
            return this.$messages.error(res.msg);
          }
          this.$messages.success("删除成功");
          this.getListWay();
        })
        .catch((err) => {
          this.loading = false;
          this.$messages.error(err.msg);
        });
    },
    eidtStatusWay(row) {
      this.loading = true;
      editApp({ id: row.id, status: row.status })
        .then((res) => {
          this.loading = false;
          if (res.code !== 200 || res.status !== 1) {
            this.getListWay();
            this.$messages.error(res.msg);
            return;
          }
          this.$messages.success("修改成功");
          this.getListWay();
        })
        .catch(() => {
          this.loading = false;
          this.$messages.error("修改失败");
          this.getListWay();
        });
    },
    editWay(row) {
      this.handleClose();
      this.title = "编辑" + row.version + "版本";
      this.dialogVisible = true;
      this.form.id = row.id;
      this.form.app_type = row.app_type;
      this.form.version = row.version;
      this.form.apk_url = row.apk_url;
      this.file = { url: row.apk_url };
      // this.contentBool = true;
      this.form.content = row.content;
      this.$nextTick(() => {
        this.file = { url: row.apk_url };
      });
    },
    addWay() {
      this.title = "添加新版本";
      this.handleClose();
      this.dialogVisible = true;
      // this.contentBool = true;
    },
    handleClose() {
      this.form = {
        version: "",
        apk_url: "",
        content: "",
        app_type: 1,
      };
      // this.contentBool = false;

      if (this.$refs.form) {
        this.$refs.form.resetFields();
      }
      this.$nextTick(() => {
        this.file = { url: "" };
        console.log(this.file);
      });
    },
    submitWay() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.dialogLoading = true;
          saveApp(this.form).then((res) => {
            this.dialogLoading = false;
            if (res.code !== 200 || res.status !== 1) {
              return this.$messages.error(res.msg);
            }
            this.$messages.success(res.msg);
            this.handleClose();
            this.dialogVisible = false;
            this.getListWay();
          });
        } else {
          return false;
        }
      });
    },
    avatarCallback(file) {
      this.form.apk_url = file.url;
      //   this.file = {
      //     url: file.url,
      //   };
    },
    editorclick(e) {
      this.form.content = e;
    },
  },
};
</script>

<style>
</style>
